package org.jprogger.myworld.api.response;

import org.jprogger.myworld.api.ApiServiceInvocationException;
import org.jprogger.myworld.api.request.ApiRequest;


/**
 * 
 * @author jprogger
 *
 */
public abstract class AbstractApiResponseHandler<T> {

	private ApiResponseErrorValidator validator = new ApiResponseErrorValidator();
	
	/**
	 * 
	 * @param response
	 * @param format
	 * @return
	 * @throws ParseResponseException
	 */
	protected ApiResponseError validateResponse(String response, String format) 
			throws ParseResponseException{
		
		ApiResponseError error = null;
		validator.validate(response, format);
		if (validator.hasError())
		{
			error = new DefaultApiResponseError();
			error.setCode(validator.getErrorCode());
			error.setMessage(validator.getErrorMessage());
		}
		return error;
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ApiServiceInvocationException
	 */
	public abstract ApiResponse<T> handleResponse(ApiRequest request, String response) 
			throws ApiServiceInvocationException;
}
